var vm = new Vue(
    {
    el: '#rrapp',
    data: {
        query: {
            randomType: 1,
        },
        loading: null,
        openEdit: false,
        randomTypeList: [
            {value: 1, label: '普通订单 万元以下优惠'},
            {value: 2, label: '普通订单 万元以上优惠'},
            {value: 3, label: '报价订单 万元以下优惠'},
            {value: 4, label: '报价订单 万元以上优惠'},
        ],
        orderRandomConfig: [], //配置列表
        orderRandomConfigBackUp: [], // 配置列表备份
    },
    mounted: function () {
        this.queryList();
    },
    methods: {
        // 查询
        queryList: function () {
            var queryParam = this.query;
            const loading = this.$loading({
                lock: true,
                text: '数据加载中!',
                spinner: 'el-icon-loading',
            });
            //ajax 请求
            $.ajax({
                type: "POST",
                url: baseURL + "order/orderrandomconfig/randomConfigList",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    loading.close();
                    if (r.code == 0) {
                        vm.orderRandomConfig = r.randomConfigList;
                        //深度克隆 备份数据
                        var newObject = jQuery.extend(true, [], r.randomConfigList);
                        vm.orderRandomConfigBackUp = newObject;
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },
        batchUpdate: function () {
            //循环校验表单
            for (var index in vm.orderRandomConfig){
                var flag = true;
                this.$refs['minRandomForm'+vm.orderRandomConfig[index].discountId].validate((valid) => {
                    if (!valid) {
                        flag = false;
                    }
                });
                this.$refs['maxRandomForm'+vm.orderRandomConfig[index].discountId].validate((valid) => {
                    if (!valid) {
                        flag = false;
                    }
                });
                this.$refs['minRandomAmountForm'+vm.orderRandomConfig[index].discountId].validate((valid) => {
                    if (!valid) {
                        flag = false;
                    }
                });
                this.$refs['maxRandomAmountForm'+vm.orderRandomConfig[index].discountId].validate((valid) => {
                    if (!valid) {
                        flag = false;
                    }
                });
            }
            if(!flag) return;

            var url = "order/orderrandomconfig/batchUpdate";
            var postParams = {
                orderRandomConfigList: vm.orderRandomConfig,
                orderRandomConfigBackUpList:  vm.orderRandomConfigBackUp
            };
            this.$confirm('确认批量更新, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                const loading = this.$loading({
                    lock: true,
                    text: '批量更新中，请勿刷新页面!',
                    spinner: 'el-icon-loading',
                });
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(postParams),
                    success: function (r) {
                        loading.close();
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.queryList();
                                vm.openEdit =  false;
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消批量更新'
                });
            });


        },

        /*
         * 最小/达优惠比例/金额 校验
         * 1.大于0 4位小数
         * 2.小于最大优惠比例金额
         * */
        validateMinRandom: function (rule,value,callback) {
            var reg = /^[+-]?\d+\.\d{0,4}$/i;
            if (value === '') {
                callback(new Error('不能为空!'));
            }else if (isNaN(value)){
                callback(new Error('请输入数字值'));
            }else if ( rule.data.minRandom.toString().split(".").length > 1 && rule.data.minRandom.toString().split(".")[1].length >4){
                callback(new Error('小数位不超过4位!'));
            }else if (rule.data.minRandom <=0){
                callback(new Error('必须大于0!'));
            }else if (rule.data.minRandom >= rule.data.maxRandom) {
                callback(new Error('必须小于最大优惠比例!'));
            } else {
                callback();
            }
        },
        /*
         * 最小/达优惠比例/金额 校验
         * 1.大于0 4位小数
         * 2.小于最大优惠比例金额
         * */
        validateMaxRandom: function (rule,value,callback) {
            if (value === '') {
                callback(new Error('不能为空!'));
            }else if (isNaN(value)){
                callback(new Error('请输入数字值'));
            }else if (rule.data.maxRandom.toString().split(".").length > 1 && rule.data.maxRandom.toString().split(".")[1].length >4){
                callback(new Error('小数位不超过4位!'));
            }else if (rule.data.maxRandom <=0){
                callback(new Error('必须大于0!'));
            } else if (rule.data.maxRandom <= rule.data.minRandom) {
                callback(new Error('必须大于最小优惠比例!'));
            } else {
                callback();
            }
        },

        /*
        * 最小/大优惠金额 校验
        * 1.大于0 2位小数
        * 2.小于最大优惠比例金额
        *
       */
        validateMinRandomAmount: function (rule,value,callback) {
            if (value === '') {
                callback(new Error('不能为空!'));
            }else if (isNaN(value)){
                callback(new Error('请输入数字值'));
            }else if (rule.data.minRandomAmount.toString().split(".").length > 1 && rule.data.minRandomAmount.toString().split(".")[1].length >2){
                callback(new Error('小数位不超过2位!'));
            }else if (rule.data.minRandomAmount <=0){
                callback(new Error('必须大于0!'));
            }else if (rule.data.minRandomAmount >= rule.data.maxRandomAmount) {
                callback(new Error('必须小于最大优惠金额限制!'));
            } else {
                callback();
            }
        },
        /*
        * 最小/大优惠金额 校验
        * 1.大于0 2位小数
        * 2.小于最大优惠比例金额
        *
       */
        validateMaxRandomAmount: function (rule,value,callback) {
            if (value === '') {
                callback(new Error('不能为空!'));
            }else if (isNaN(value)){
                callback(new Error('请输入数字值'));
            }else if (rule.data.maxRandomAmount.toString().split(".").length > 1 && rule.data.maxRandomAmount.toString().split(".")[1].length >2){
                callback(new Error('小数位不超过2位!'));
            }else if (rule.data.maxRandomAmount <=0){
                callback(new Error('必须大于0!'));
            } else if (rule.data.maxRandomAmount <= rule.data.minRandomAmount) {
                callback(new Error('必须大于最小优惠金额限制!'));
            } else {
                callback();
            }
        },

    }
});